<?php

class Rate{
	var $source;
	var $title;
	var $code;
	var $name;
	var $buy;
	var $transfer;
	var $sell;
	function getXML(){
	 $c = curl_init($this->source);
	 curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
	 $page = curl_exec($c);
	 curl_close($c);
		//$xml = file_get_contents($this->source);*/
		return $page;
		//die("Cannot connnect to webservice");
	}
	function parse(){
		ob_start();
		header("Content-type: text/xml; charset=UTF-8");
		$xmlData = NULL;
		$p = xml_parser_create();
		xml_parse_into_struct($p,$this->getXML() , $xmlData);
		xml_parser_free($p);
		//Data
		$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
		$xml .= "\r\n<spacegate>\r\n";
		$xml .= '<source>'.$this->title.'</source>'."\r\n";
		$xml .= '<date>'.$xmlData[1]['value'].'</date>'."\r\n";
		//print_r($this->getXML());exit();
		if($xmlData) foreach($xmlData as $v) 
			if(isset($v['attributes'])) $xml .= '<rate '.
				'CODE="'.$v['attributes'][$this->code].'" '.
				'NAME="'.$v['attributes'][$this->name].'" '.
				'BUY="'.$v['attributes'][$this->buy].'" '.
				'TRANSFER="'.$v['attributes'][$this->transfer].'" '.
				'SELL="'.$v['attributes'][$this->sell].'" '.
				'/>'."\r\n";
		$xml .= "</spacegate>";
		die($xml);
	}
}
$rate = new Rate();
$rate->source = 'http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx';
$rate->title = 'VietCom Bank';
$rate->code = 'CURRENCYCODE';
$rate->name = 'CURRENCYNAME';
$rate->buy = 'BUY';
$rate->transfer = 'TRANSFER';
$rate->sell = 'SELL';
$rate->parse();
?>
